package top.drinyeit.nongmao.adapter;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.model.CartBean;
import top.drinyeit.nongmao.model.CartListBean;
import top.drinyeit.nongmao.retrofit.HttpResponse;
import top.drinyeit.nongmao.retrofit.RetrofitMethod;
import top.drinyeit.nongmao.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.nongmao.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.nongmao.app.Constant.COMMON_SUCCESS;

/**
 * Created by Administrator on 2018/2/2.
 */

public class CategoryCartAdapter extends RecyclerView.Adapter<CategoryCartAdapter.ViewHolder> {
    private Context context;
    private CartListBean data;
    private List<CartBean> mData = null;
    private boolean isDeleteAble = true;
    private Handler mHandler;

    public CategoryCartAdapter(CartListBean data, android.os.Handler mHandler) {
        this.data = data;
        this.mHandler = mHandler;
        setMData(data);
    }

    public void clear() {
        mData.clear();
    }

    public List<CartListBean.CartShopBean> getData() {
        return data.getShopping_cart_list();
    }

    public List<CartBean> getmData() {
        return mData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context = parent.getContext();
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_category_cart, parent, false));
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        holder.name.setText(mData.get(position).getCommodity_name());
        BigDecimal bigDecimal = new BigDecimal(mData.get(position).getCommodity_price() * mData.get(position).getCommodity_sum());
        holder.money.setText("¥" + bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP));
        holder.number.setText(String.valueOf(mData.get(position).getCommodity_sum()));
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public TextView money, number, name;
        public ImageView add, subtract;

        public ViewHolder(View itemView) {
            super(itemView);
            add = itemView.findViewById(R.id.iv_add);
            subtract = itemView.findViewById(R.id.iv_subtract);
            money = itemView.findViewById(R.id.tv_money);
            number = itemView.findViewById(R.id.tv_number);
            name = itemView.findViewById(R.id.tv_category_name);

            add.setOnClickListener(this);
            subtract.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.iv_add:
                    add(getAdapterPosition(), mData.get(getAdapterPosition()).getId() + "");
                    break;
                case R.id.iv_subtract:
                    if (mData.get(getAdapterPosition()).getCommodity_sum() > 1) {
                        subtract(getAdapterPosition(), mData.get(getAdapterPosition()).getId() + "");
                    } else {
                        if (isDeleteAble) {
                            isDeleteAble = false;
                            delete(getAdapterPosition(), mData.get(getAdapterPosition()).getId());
                        }
                    }
                    break;
            }
        }
    }

    private void setMData(CartListBean data) {
        this.mData = new ArrayList<>();
        for (int i = 0; i < data.getShopping_cart_list().size(); i++) {
            mData.addAll(data.getShopping_cart_list().get(i).getCommodity_list());
        }
    }

    //添加数量
    public void add(final int itemCount, String id) {
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                Toast.makeText(context, "失败,请重新操作", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                if (o.getCode() == COMMON_SUCCESS) {
                    mData.get(itemCount).setCommodity_sum(mData.get(itemCount).getCommodity_sum() + 1);
                    notifyDataSetChanged();

                } else {
                    Toast.makeText(context, o.getMessage(), Toast.LENGTH_SHORT).show();
                }
                Message message = Message.obtain(mHandler, 0x123);
                message.sendToTarget();
            }
        });
        RetrofitMethod.getInstance().changeCartNumber(subscriber, id, mData.get(itemCount).getCommodity_sum() + 1);
    }

    //减少数量
    public void subtract(final int itemCount, String id) {
        CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                Toast.makeText(context, "失败,请重新操作", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                if (o.getCode() == COMMON_SUCCESS) {
                    mData.get(itemCount).setCommodity_sum(mData.get(itemCount).getCommodity_sum() - 1);
                    notifyDataSetChanged();
                    Message message = Message.obtain(mHandler, 0x123);
                    message.sendToTarget();
                } else {
                    Toast.makeText(context, o.getMessage(), Toast.LENGTH_SHORT).show();
                }

            }
        });
        RetrofitMethod.getInstance().changeCartNumber(subscriber, id, mData.get(itemCount).getCommodity_sum() - 1);
    }

    //删除商品
    public void delete(final int itemCount, final int id) {
        final CommonSubscriber<HttpResponse<String>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<String>>() {
            @Override
            public void onError(String e, int code) {
                Toast.makeText(context, "失败,请重新操作", Toast.LENGTH_SHORT).show();
                isDeleteAble = true;
            }

            @Override
            public void onNext(HttpResponse<String> o) {
                if (o.getCode() == COMMON_SUCCESS) {
                    mData.remove(itemCount);
//                    notifyItemRemoved(itemCount);
//                    notifyItemRangeRemoved(0, mData.size());
                    notifyDataSetChanged();
                    Message message = Message.obtain(mHandler, 0x123);
                    message.sendToTarget();
                } else {
                    Toast.makeText(context, o.getMessage(), Toast.LENGTH_SHORT).show();
                }

                isDeleteAble = true;
            }

        });
        RetrofitMethod.getInstance().deleteCart(subscriber, id + "");
    }


    private int getIDCount(int id) {
        for (int i = 0; i < mData.size(); i++) {
            if (id == mData.get(i).getId()) {
                return i;
            }
        }
        return -1;
    }
}
